package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/microcosm-cc/bluemonday"
	"github.com/russross/blackfriday"
	"io/ioutil"
	"os"
	"softwareEngineering/models"
)

var FileUploadSql = func(user models.User, title string, path string) {
	o := orm.NewOrm()
	article := models.Article{}
	article.Uid = user.Id
	article.Title = title
	article.Content = readFile(path)
	article.Path = path

	_, err := o.Insert(&article)
	if err != nil {
		beego.Info("上传成功")
	}
}

var ImageUploadSql = func(user models.User, name string, path string) {
	o := orm.NewOrm()
	image := models.Image{}
	image.Uid = user.Id
	image.Name = name
	image.Path = path

	_, err := o.Insert(&image)
	if err != nil {
		beego.Info("上传图片成功")
	}

}

func QueryAllArticle(user models.User) (ats []models.Article) {
	o := orm.NewOrm()
	articles := []models.Article{}
	qs := o.QueryTable("article")
	n, err := qs.Filter("Uid", user.Id).All(&articles)
	if err == nil && n > 0 {
		return articles
	}
	return
}

func QueryArticleById(id int) (article models.Article) {
	o := orm.NewOrm()
	art := models.Article{}
	qs := o.QueryTable("article")
	n, err := qs.Filter("id", id).All(&art)
	if err == nil && n > 0 {
		return art
	}
	return

}

func readFile(path string) (ct string) {
	f, err := os.Open(path)
	if err != nil {
		return
	}

	b, erro := ioutil.ReadAll(f)
	if erro != nil {
		return
	}

	return string(b)
}

//针对文章的相关操作
func EditArticle() {

}

func DelArticle(id int) bool {
	o := orm.NewOrm()
	article := models.Article{}
	article.Id = id
	_, err := o.Delete(&article)
	if err != nil {
		return false
	}
	return true
}

//针对视频的相关操作

func DelVideo(id int) bool {
	o := orm.NewOrm()
	video := models.Video{}
	video.Id = id
	_, err := o.Delete(&video)
	if err != nil {
		return false
	}

	return false

}

//video operations end

func QueryAllImage(user models.User) []models.Image {
	o := orm.NewOrm()
	var images []models.Image
	is := o.QueryTable("image")
	n, err := is.Filter("Uid", user.Id).All(&images)
	if err == nil && n > 0 {
		return images
	}
	return nil
}
func QueryImageById(id string) (models.Image, bool) {
	o := orm.NewOrm()
	var images models.Image
	is := o.QueryTable("image")
	n, err := is.Filter("id", id).All(&images)
	if err == nil && n > 0 {
		return images, true
	}
	return images, false
}

//删除图片功能
func DeleImageById(id int) bool {
	o := orm.NewOrm()
	images := models.Image{}
	images.Id = id
	_, err := o.Delete(&images)
	if err != nil {
		return false
	}
	return true
}

func ArticleContent(ArticleId int) (string, string) {
	beego.Info(ArticleId)
	input := []byte("")
	o := orm.NewOrm()
	article := models.Article{}
	qs := o.QueryTable("article")
	beego.Info(qs.Exist())
	n, err := qs.Filter("id", ArticleId).All(&article)
	if err == nil && n > 0 {
		input = []byte(article.Content)
	} else {
		return "信息有误", "信息有误"
	}
	unsafe := blackfriday.MarkdownBasic(input)
	html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
	return string(html), string(input)
}

//查询数据库中的Book数据，并返回
func QueryBookData() []models.Book {
	var books []models.Book
	o := orm.NewOrm()
	qs := o.QueryTable("book")
	n, err := qs.Filter("deploy", "1").All(&books)
	if n > 0 && err == nil {
		return books
	}
	return nil
}

func QueryBooKPageContentById(id string) []models.Book {
	var page []models.Book
	o := orm.NewOrm()
	qs := o.QueryTable("book")
	n, err := qs.Filter("id", id).All(&page)
	if n > 0 && err == nil {
		return page
	}

	return nil

}

func UpdateInfor(id int, name string, pwd string, role string, imgId string) bool {
	o := orm.NewOrm()
	user := models.User{}
	user.Id = id
	user.Name = name
	user.Pwd = pwd
	user.Role = role
	user.ImgId = imgId

	_, err := o.Update(&user, "name", "Pwd", "imgId")
	if err == nil {
		return true
	}
	return false
}

func UploadVideo(uid int, name string, path string, poster string, ty string) bool {
	o := orm.NewOrm()
	video := models.Video{}
	video.Uid = uid
	video.Name = name
	video.Path = path
	video.Poster = poster
	video.Type = ty
	_, err := o.Insert(&video)
	if err != nil {
		return false
	}
	return true
}

func QueryVideoByUserId(uid string) []models.Video {
	o := orm.NewOrm()
	videos := []models.Video{}

	qs := o.QueryTable("video")
	n, err := qs.Filter("uid", uid).All(&videos)
	if err == nil && n > 0 {
		return videos
	} else {
		return nil
	}

}
